package model;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Map;


public class DAOPersist implements Persistence{
	private static final String DB_URL = "jdbc:mysql://localhost:3306/opensocial";
	private static final String DB_UID = "opensocial";
	private static final String DB_PW = "opensocial";
	private static final String DB_DRIVER = "com.mysql.jdbc.Driver";
	
	private static DAOPersist _instance;
	public static DAOPersist get(){
		if(_instance == null){
			_instance = new DAOPersist();
		}
		return _instance;
	}

	private DAOPersist(){
	}	
	/*
	 * executeSQLQuery - A utility method to execute a SQL query against a JDBC
	 * source and returns a ResultSet.
	 */
	public ResultSet executeSQLQuery(String query)
	throws ClassNotFoundException {
		Statement stmt;
		ResultSet rs = null;

		System.out.println("query: " + query);

		try {
			Class.forName(DB_DRIVER);
			Connection con = DriverManager.getConnection(DB_URL, DB_UID, DB_PW);

			stmt = (Statement) con.createStatement();
			rs = stmt.executeQuery(query);
		}

		catch (SQLException ex) {
			// handle any errors
			System.out.println("SQLException: " + ex.getMessage());
			System.out.println("SQLState: " + ex.getSQLState());
			System.out.println("VendorError: " + ex.getErrorCode());
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return rs;
	}
	
	/*
	 * executeSQLQuery - A utility method to execute a SQL query against a JDBC
	 * source and returns a ResultSet.
	 */
	public ResultSet executeSQLUpdate(String statement){
		Statement stmt;
		ResultSet rs = null;

		System.out.println("executing: " + statement);
		try {
			Class.forName(DB_DRIVER);
			Connection con = DriverManager.getConnection(DB_URL, DB_UID, DB_PW);

			stmt = (Statement) con.createStatement();
			stmt.executeUpdate(statement, stmt.RETURN_GENERATED_KEYS);
			rs = stmt.getGeneratedKeys();
		} catch (SQLException ex) {
			// handle any errors
			System.out.println("SQLException: " + ex.getMessage());
			System.out.println("SQLState: " + ex.getSQLState());
			System.out.println("VendorError: " + ex.getErrorCode());
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return rs;
	}
	public void init(SaleData data){
		String hql = "insert into saledata()";
//		String 
	}
	public void save(SaleData data){
		String hql = "update saledata set " +
				" price=" + data.getPrice() +
				" cash=" + data.getCash() +
				" slaves=" + data.getSlaves() +
				" masterid=" + data.getMaster().getId() + 
				" history=" + data.getHistory() +
				" nickname=" + data.getNickname() +
				" buyutil=" + data.getBuyUntil() +
				" status=" + data.getStatus() +
				" where id=" + data.getId();
		
		ResultSet rs = executeSQLUpdate(hql);
//		rs.get
		
	}
	
	//load data use userid
	public SaleData load(String id){
		
		return null;
	}
	
	//load all data into memory
	public Map<String,SaleData> loadAll(){
		
		return null;
	}
}
